<?php
class ModelToolCollection extends Model {
	public function addNode($data) {
		$this->_data($data);		
		return $this->db->insert('collection_node', $data);
	}
	
	public function editNode($id, $data) {
		$this->_data($data);
		return $this->db->update('collection_node', $data, "id = $id");
	}
	
	protected function _data(&$data) {
		
		if (isset($data['name_func'])) {
			$data['name_func'] = is_array($data['name_func']) ? implode('|', $data['name_func']) : '';
			if ($data['custom_name_func']) $data['name_func'] .= '|' . $data['custom_name_func'];
		}
		
		if (isset($data['sku_func'])) {
			$data['sku_func'] = is_array($data['sku_func']) ? implode('|', $data['sku_func']) : '';
			if ($data['custom_sku_func']) $data['sku_func'] .= '|' . $data['custom_sku_func'];
		}
		
		if (isset($data['price_func'])) {
			$data['price_func'] = is_array($data['price_func']) ? implode('|', $data['price_func']) : '';
			if ($data['custom_price_func']) $data['price_func'] .= '|' . $data['custom_price_func'];
		}
		
		if (isset($data['weight_func'])) {
			$data['weight_func'] = is_array($data['weight_func']) ? implode('|', $data['weight_func']) : '';
			if ($data['custom_weight_func']) $data['weight_func'] .= '|' . $data['custom_weight_func'];
		}
		
		if (isset($data['image_func'])) {
			$data['image_func'] = is_array($data['image_func']) ? implode('|', $data['image_func']) : '';
			if ($data['custom_image_func']) $data['image_func'] .= '|' . $data['custom_image_func'];
		}
		
		if (isset($data['description_func'])) {
			$data['description_func'] = is_array($data['description_func']) ? implode('|', $data['description_func']) : '';
			if ($data['custom_description_func']) $data['description_func'] .= '|' . $data['custom_description_func'];
		}
		
		if (isset($data['other1_func'])) {
			$data['other1_func'] = is_array($data['other1_func']) ? implode('|', $data['other1_func']) : '';
			if ($data['custom_other1_func']) $data['other1_func'] .= '|' . $data['custom_other1_func'];
		}
		
		if (isset($data['other2_func'])) {
			$data['other2_func'] = is_array($data['other2_func']) ? implode('|', $data['other2_func']) : '';
			if ($data['custom_other2_func']) $data['other2_func'] .= '|' . $data['custom_other2_func'];
		}
		
		if (isset($data['other3_func'])) {
			$data['other3_func'] = is_array($data['other3_func']) ? implode('|', $data['other3_func']) : '';
			if ($data['custom_other3_func']) $data['other3_func'] .= '|' . $data['custom_other3_func'];
		}
	}
	
	public function deleteNode($id) {
		if (is_array($id)) $id = implode(',', $id);
		$this->db->runSql("DELETE FROM @@collection_content WHERE nodeid IN ($id)");
		$this->db->runSql("DELETE FROM @@collection_node WHERE id IN ($id)");
	}
	
	public function getNode($id) {
		$node = $this->db->queryOne("SELECT * FROM @@collection_node WHERE id = $id");
		return v5_html_entity_decode($node);
	}
	
	public function getNodes($filter = null) {
		$sql = "SELECT id,site_name,site_url,node_title,last_time,total,status FROM @@collection_node";
		if ($filter) {
			$sql .= $filter->limit();
		}
		$query = $this->db->query($sql);
		return $query->rows;
	}
	
	public function clearNode($id) {
		$data = array("status" => 0, "last_time" => 0, "total"=>0);
		$this->db->update('collection_node', $data, "id=$id");
		$this->db->runSql("DELETE FROM @@collection_content WHERE nodeid = $id");
	}
	
	public function getTotalNodes() {
		return $this->db->queryOne("SELECT count(*) FROM @@collection_node");
	}
	
	public function saveUrls($nodeid, $data) {
		$md5 = array();
		foreach($data as $item) {
			$md5[] = $item['urlmd5'];
		}
		$md5 = implode("','", $md5);
		$md5 = $this->db->queryArray("SELECT urlmd5 FROM @@collection_content WHERE urlmd5 IN ('$md5')");
		
		$inserts = array();
		$f = 0;
		foreach($data as $item) {
			if(in_array($item['urlmd5'], $md5)) continue;
			$f++;
			$md5[] = $item['urlmd5'];
			$item['nodeid'] = $nodeid;
			$inserts[] = $item;
		}
		if (!$f) return 0;
		$this->db->insert('collection_content', $inserts);
		$this->db->runSql("UPDATE @@collection_node SET total=total + $f, status=IF(status>0,1,0) WHERE id = $nodeid");
	}
	
	public function getUrls($nodeid, $limit = 10) {
		$sql = "SELECT id,url FROM @@collection_content WHERE nodeid=$nodeid AND status=0 LIMIT $limit";
		$query = $this->db->query($sql);
		$urls = $query->rows;
		$ids = array();
		foreach($urls as $url) {
			$ids[] = $url['id'];
		}
		if ($urls) {
			$this->db->query("UPDATE @@collection_content SET status = 9 WHERE id IN (" . implode(',', $ids) . ")");
		}
		return $urls;
	}
	
	public function getContents($filter = null, $field='id, nodeid, name, url, sku, price, image, coltime, imptime, status') {
		$sql = "SELECT $field FROM @@collection_content WHERE 1";
		if ($filter) {
			if ($filter->get('nodeid')) {
				$sql .= " AND nodeid = "  . $filter->get('nodeid');
			}
			if ($filter->get('filter_name')) {
				$sql .= " AND name LIKE  '%"  . ES($filter->get('filter_name')) . "%'";
			}
			if ($filter->get('filter_status') !== null) {
				$sql .= " AND status = "  . $filter->get('filter_status');
			}
			if ($filter->get('ids')) {
				$sql .= " AND id IN (" . $filter->get('ids') . ")";
			}
			$sql .= $filter->limit();
		}
		$query = $this->db->query($sql);
		return $query->rows;
	}
	
	public function getTotalContents($filter = null) {
		$sql = "SELECT COUNT(*) FROM @@collection_content WHERE 1";
		if ($filter) {
			if ($filter->get('nodeid')) {
				$sql .= " AND nodeid = "  . $filter->get('nodeid');
			}
			if ($filter->get('filter_name')) {
				$sql .= " AND name LIKE  '%"  . ES($filter->get('filter_name')) . "%'";
			}
			if ($filter->get('filter_status') !== null) {
				$sql .= " AND status = "  . $filter->get('filter_status');
			}
		}
		return $this->db->queryOne($sql);
	}
	
	public function getContent($id) {
		return $this->db->queryOne("SELECT * FROM @@collection_content WHERE id = $id");
	}
	
	public function editContent($id, $data) {
		return $this->db->update('collection_content', $data, "id=$id");
	}
	
	public function deleteContent($id, $nodeid) {
		$i = 1;
		if (is_array($id)) {
			$i = count($id);
			$id = implode(',', $id);
		}
		$this->db->runSql("DELETE FROM @@collection_content WHERE id IN ($id)");
		$this->db->runSql("UPDATE @@collection_node SET total = total - $i WHERE id = $nodeid");
	}
	
	public function resetContent($id) {
		if (is_array($id)) {
			$id = implode(',', $id);
		}
		$this->db->query("UPDATE @@collection_content SET sku='',price='',weight='',other1='',other2='',other3='',image='',description='',coltime=0,imptime=0,status=0 WHERE id IN ($id)");
	}
}